﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Security.AccessControl;
using System.Windows.Forms;
using PiCamera;
using PiFramework;


namespace PiControls.Controls.DriveUI.CameraUI
{
    public partial class CameraControl : UserControl
    {

        private string m_objectName = "";
        private string m_objectClass = "";
        private int m_exposure = 0;
        private PiCameraBase m_camera = null;

        public int Exposure
        {
            get { return m_exposure; }
            set { m_exposure = value; }
        }

        public string ObjectName
        {
            get { return m_objectName; }
            set { m_objectName = value; }
        }

        public string ObjectClass
        {
            get { return m_objectClass; }
            set { m_objectClass = value; }
        }

        private void SetCtrlWhenStopGrab()
        {
            bnStartGrab.Enabled = true;
            bnStopGrab.Enabled = false;

            bnTriggerExec.Enabled = false;


            bnSaveBmp.Enabled = false;
            bnSaveJpg.Enabled = false;
            bnSaveTiff.Enabled = false;
            bnSavePng.Enabled = false;
        }


        public CameraControl(string objectName, string objectClass, int exposure)
        {
            InitializeComponent();
            this.m_objectName = objectName;
            this.m_objectClass = objectClass;
            this.m_exposure = exposure;
            m_camera = PCameraControlHelp.Instance.GetCamera(objectName);
            if (m_camera == null)
            {
                Console.WriteLine("Camera not found");
                return;
            }

            if (!m_camera.isConnected())
            {


                SetCtrlWhenClose();
                return;
            }


            SetCtrlWhenOpen();
            Console.WriteLine("相机已经连接");
            Control.CheckForIllegalCrossThreadCalls = false; //加载时 取消跨线程检查
            m_camera.SetPictureBox(pictureBox1);

        }
        private void SetCtrlWhenClose()
        {
            bnOpen.Enabled = true;

            bnClose.Enabled = false;
            bnStartGrab.Enabled = false;
            bnStopGrab.Enabled = false;
            bnContinuesMode.Enabled = false;
            bnTriggerMode.Enabled = false;
            cbSoftTrigger.Enabled = false;
            bnTriggerExec.Enabled = false;

            bnSaveBmp.Enabled = false;
            bnSaveJpg.Enabled = false;
            bnSaveTiff.Enabled = false;
            bnSavePng.Enabled = false;
            tbExposure.Enabled = false;
            tbGain.Enabled = false;
            tbFrameRate.Enabled = false;
            bnGetParam.Enabled = false;
            bnSetParam.Enabled = false;
            tbPixelFormat.Enabled = false;
        }


        private void SetCtrlWhenOpen()
        {
            bnOpen.Enabled = false;

            bnClose.Enabled = true;
            bnStartGrab.Enabled = true;
            bnStopGrab.Enabled = false;
            bnContinuesMode.Enabled = true;
            bnContinuesMode.Checked = true;
            bnTriggerMode.Enabled = true;
            cbSoftTrigger.Enabled = false;
            bnTriggerExec.Enabled = false;

            tbExposure.Enabled = true;
            tbGain.Enabled = true;
            tbFrameRate.Enabled = true;
            bnGetParam.Enabled = true;
            bnSetParam.Enabled = true;
            tbPixelFormat.Enabled = false;
        }
        private void SetCtrlWhenStartGrab()
        {
            bnStartGrab.Enabled = false;
            bnStopGrab.Enabled = true;

            if (bnTriggerMode.Checked && cbSoftTrigger.Checked)
            {
                bnTriggerExec.Enabled = true;
            }

            bnSaveBmp.Enabled = true;
            bnSaveJpg.Enabled = true;
            bnSaveTiff.Enabled = true;
            bnSavePng.Enabled = true;
        }


        private void bnOpen_Click(object sender, EventArgs e)
        {
           bool isOpen = m_camera.OpenCamera(m_objectName);

            if (!isOpen)
            {
              MessageBox.Show("相机打开失败");
                return;
            }
            SetCtrlWhenOpen();
        }

        private void bnClose_Click(object sender, EventArgs e)
        {
            m_camera.CloseCamera();
            SetCtrlWhenClose();
        }

        private void CameraControl_Load(object sender, EventArgs e)
        {

            foreach (Control ctrl in GetAllControls(this))
            {
                if (ctrl.GetType() == typeof(Button))
                {
                    Button btn = (Button)ctrl;
                }
            }
        }

        private IEnumerable<Control> GetAllControls(Control control)
        {
            foreach (Control child in control.Controls)
            {
                // 返回当前子控件
                yield return child;
                // 递归返回子控件的子控件
                foreach (Control descendant in GetAllControls(child))
                {
                    yield return descendant;
                }
            }
        }

        private void bnStartGrab_Click(object sender, EventArgs e)
        {
            m_camera.StartGrab();
            if (!m_camera.isGrabbing())
            {
                Console.WriteLine("相机启动失败");
                return;
            }
            SetCtrlWhenStartGrab();
        }

        private void bnStopGrab_Click(object sender, EventArgs e)
        {
            if (!m_camera.isGrabbing())
            {
                Console.WriteLine("相机未启动");
                return;
            }
            m_camera.StopGrab();
            SetCtrlWhenStopGrab();
        }

        private void bnTriggerMode_CheckedChanged(object sender, EventArgs e)
        {
            if (bnTriggerMode.Checked)
            {
                m_camera.setTriggerMode(0);

                if (cbSoftTrigger.Checked)
                {

                    bnTriggerExec.Enabled = true;
                }


             }
            cbSoftTrigger.Enabled = true;
        }

        private void bnContinuesMode_CheckedChanged(object sender, EventArgs e)
        {
            cbSoftTrigger.Enabled = false;
            m_camera.setTriggerMode(1);
        }

        private void bnTriggerExec_Click(object sender, EventArgs e)
        {
           
            m_camera.TriggerOnceExec();
        }

        private void cbSoftTrigger_CheckedChanged(object sender, EventArgs e)
        {

            if (cbSoftTrigger.Checked)
            {

                bnTriggerExec.Enabled = true;
            }

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void tbPixelFormat_TextChanged(object sender, EventArgs e)
        {

        }

        private void bnGetParam_Click(object sender, EventArgs e)
        {

        }

        private void bnSetParam_Click(object sender, EventArgs e)
        {

        }

        private void bnSaveJpg_Click(object sender, EventArgs e)
        {

        }

        private void bnSavePng_Click(object sender, EventArgs e)
        {

        }
    }
}
